<template>
  <div class="goods_list">
    <!-- 按钮 -->
    <div class="btns">
      <el-form :inline="true" size="small">
        <el-form-item>
          <el-button type="primary" size="small" @click="toSaveHandler"
            >新增</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="param.productCategoryId"
            placeholder="请选择菜品种类"
            clearable
          >
            <el-option
              v-for="item in categoryData.list"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-button size="small" icon="el-icon-search" @click="loadGoodses"
          >搜索</el-button
        >
      </el-form>
    </div>
    <!-- 按钮 -->
    <!-- 按钮 -->
    <!-- 表格 -->
    <!-- {{ goodsData }} -->
    <el-table size="small" :data="goodsData.list" v-loading="loading">
      <!-- <el-table-column
        label="序号"
        type="index"
        :index="1"
        fixed="left"
      ></el-table-column> -->
      <el-table-column label="菜品图片" width="220" fixed="left" align="center">
        <template slot-scope="scope">
          <img
            :src="scope.row.photo"
            alt=""
            style="width: 180px; cursor: pointer"
            @click="toPhotoHandler(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="名称" width="200" align="center">
        <template slot-scope="props">
          <div style="font-size: 14px">{{ props.row.name }}</div>
          <div>
            <span style="color: red; font-size: 17px"
              >{{ props.row.price }}元</span
            >
            / {{ props.row.unit }}
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="规格参数"
        prop="unit"
        width="200"
      ></el-table-column> -->

      <!-- <el-table-column label="单价" prop="price" width="80"></el-table-column> -->
      <el-table-column label="销量" prop="sales" width="80"></el-table-column>
      <el-table-column label="状态" width="100" align="center">
        <template slot-scope="props">
          <el-tag type="success" v-if="props.row.status === '正常'"
            >上架商品</el-tag
          >
          <el-tag type="danger" v-else>下架商品</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column label="菜单" prop="category.name" width="100"></el-table-column> -->
      <el-table-column label="介绍" prop="description"></el-table-column>
      <el-table-column label="操作" width="200" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button
            type="text"
            v-if="scope.row.status === '正常'"
            size="mini"
            @click="toDownHandler(scope.row)"
            >下架</el-button
          >
          <el-button
            type="text"
            v-else
            size="mini"
            @click="toUpHandler(scope.row)"
            >上架</el-button
          >

          <el-button type="text" size="mini" @click="toDeleteHandler(scope.row)"
            >删除</el-button
          >
          <el-button type="text" size="mini" @click="toEditHandler(scope.row)"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格 -->
    <!-- 分页 -->
    <el-pagination
      background
      layout="prev, pager, next"
      small
      :hide-on-single-page="true"
      :total="goodsData.total"
      :page-size="goodsData.pageSize"
      :current-page="goodsData.page"
      @current-change="currentChangeHandler"
      @size-change="handleSizeChange"
      :page-sizes="[5, 8, 10, 12]"
    >
    </el-pagination>
    <!-- 分页 -->
    <!-- 模态框 -->
    <el-dialog
      :title="title"
      :visible.sync="visible"
      width="50%"
      @close="clearValidate"
      class="customer_modal"
    >
      <div>
        <el-form
          :model="form"
          ref="goodsForm"
          :rules="rules"
          label-width="80px"
        >
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item label="单价" prop="price">
            <el-input v-model="form.price"></el-input>
          </el-form-item>
          <el-form-item label="规格参数" prop="unit">
            <el-input v-model="form.unit"></el-input>
          </el-form-item>
          <el-form-item label="所属菜单" prop="productCategoryId">
            <el-cascader
              v-model="form.productCategoryId"
              :options="categoryData.list"
              :props="{
                label: 'name',
                value: 'id',
                children: 'children',
                checkStrictly: false,
                emitPath: false,
              }"
            ></el-cascader>
          </el-form-item>
          <!-- <el-form-item label="店铺" prop="shopId">
            <el-select v-model="form.shopId">
              <el-option
                v-for="c in shopData"
                :label="c.name"
                :value="c.id"
                :key="c.id"
              ></el-option>
            </el-select>
          </el-form-item> -->
          <el-form-item label="介绍" prop="description">
            <el-input type="textarea" v-model="form.description"></el-input>
          </el-form-item>
          <el-form-item label="商品主图" prop="phtot">
            <el-upload
              ref="upload"
              class="upload-demo"
              :action="this.baseURL + '/file/upload'"
              :on-success="uploadSuccessHandler"
              :file-list="fileList"
            >
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">文件大小不允许超过3M</div>
            </el-upload>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="visible = false">取消</el-button>
        <el-button size="small" type="primary" @click="submitHandler"
          >确定</el-button
        >
      </span>
    </el-dialog>
    <!-- 模态框 -->
    <!-- 图片详情 -->
    <el-dialog
      title="商品主图"
      :visible.sync="visible_photo"
      width="50%"
      class="customer_modal"
    >
      <div>
        <img :src="form.photo" width="100%" alt="" />
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="visible_photo = false">关闭</el-button>
      </span>
    </el-dialog>
    <!-- /图片详情 -->
  </div>
</template>
<script>
import { get, post } from "@/utils/request";
import _ from "lodash";
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      visible: false,
      visible_photo: false,
      loading: false,
      title: "新增商品信息",
      param: {
        page: 1,
        pageSize: 5,
      },
      fileList: [],
      goodsData: { list: [] },
      categoryData: { list: [] },
      // brandData: { list: [] },
      // shopData: [],
      form: {},
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        unit: [{ required: true, message: "请输入规格参数", trigger: "blur" }],
        price: [{ required: true, message: "请输入单价", trigger: "blur" }],
        productCategoryId: [
          { required: true, message: "请选择所属分类", trigger: "change" },
        ],
      },
    };
  },
  created() {
    // this.loadShops();
    this.loadGoodses();
    this.loadCategorys();
    // this.loadBrands();
  },
  computed: { ...mapGetters(["user"]) },
  methods: {
    // 加载店铺
    // loadShops() {
    //   let url = "/shop/query";
    //   get(url, { userId: this.user.id }).then((response) => {
    //     this.shopData = response.data;
    //   });
    // },
    // 上传成功
    uploadSuccessHandler(response) {
      if (response.status == 200) {
        this.$set(
          this.form,
          "photo",
          "http://121.199.29.84:8001/group1/" + response.data.id
        );
      } else {
        this.$message({ type: "error", message: "附件服务器异常！" });
        this.visible = false;
      }
    },
    // 去查看图片详情
    toPhotoHandler(row) {
      this.form = row;
      this.visible_photo = true;
    },
    submitHandler() {
      // console.log(this.$refs["goodsForm"]);
      this.$refs["goodsForm"].validate((valid) => {
        if (valid) {
          let url = "/product/saveOrUpdate";
          post(url, this.form).then((response) => {
            this.$message({ type: "success", message: response.message });
            this.loadGoodses();
            this.visible = false;
          });
        } else {
          return false;
        }
      });
    },
    // 去保存
    toSaveHandler() {
      this.title = "新增菜品信息";
      this.form = {};
      this.fileList = [];
      this.visible = true;
    },
    // 去修改
    toEditHandler(row) {
      this.title = "修改菜品信息";
      this.form = _.clone(row);
      this.visible = true;
    },
    // 去上架
    toUpHandler(row) {
      this.$confirm("此操作会将商品上架, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let url = "/product/online";
        get(url, { id: row.id }).then((response) => {
          this.$message({ type: "success", message: "上架成功!" });
          this.loadGoodses();
        });
      });
    },
    // 去下架
    toDownHandler(row) {
      this.$confirm("此操作会将商品下架, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let url = "/product/offline";
        get(url, { id: row.id }).then((response) => {
          this.$message({ type: "success", message: "下架成功!" });
          this.loadGoodses();
        });
      });
    },
    // 去删除
    toDeleteHandler(row) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let url = "/product/deleteById";
        get(url, { id: row.id }).then((response) => {
          this.$message({ type: "success", message: "删除成功!" });
          this.loadGoodses();
        });
      });
    },
    // 查询商品
    loadGoodses() {
      this.loading = true;
      let url = "/product/pageQuery";
      get(url, this.param).then((response) => {
        this.goodsData = response.data;
        this.loading = false;
      });
    },
    // 查询菜单
    loadCategorys() {
      let url = "/productCategory/pageQuery";
      get(url, {
        page: 1,
        pageSize: 100,
      }).then((response) => {
        this.categoryData = response.data;
      });
    },
    // loadBrands() {
    //   this.loading = true;
    //   let url = "/brand/pageQuery";
    //   get(url, {
    //     page: 1,
    //     pageSize: 100,
    //   }).then((response) => {
    //     this.brandData = response.data;
    //   });
    // },
    // 当前页发生改变
    currentChangeHandler(page) {
      this.param.page = page;
      this.loadGoodses();
    },
    // 当前页长度发生改变
    handleSizeChange(pageSize) {
      this.param.pageSize = pageSize;
      this.loadGoodses();
    },
    // 重置表单校验
    clearValidate() {
      this.$refs["goodsForm"].clearValidate();
      this.$refs["upload"].clearFiles();
    },
  },
};
</script>
<style lang="scss" scoped>
.el-pagination {
  margin-top: 0.3em;
}
</style>
